package liuliu.throughwaring.ui.activity;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.widget.TextView;

import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;

import com.ivsign.android.IDCReader.CopyFile;

import net.tsz.afinal.annotation.view.CodeNote;

import liuliu.throughwaring.control.settting.ISettingView;
import liuliu.throughwaring.control.settting.SettingListener;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.ModifiedModel;
import liuliu.throughwaring.model.PersonModel;
import liuliu.throughwaring.model.PoliceModel;
import liuliu.throughwaring.model.TerminalModel;

/**
 * Created by liuliu on 2015/11/13   14:41
 * <p/>
 * 加载页面
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class LoadingActivity extends BaseActivity implements ISettingView {
    public static LoadingActivity mInstance;
    SettingListener settingListener;
    SpannableString msp = null;
    @CodeNote(id = R.id.loading_xieti)
    TextView txt;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_loading);
        msp = new SpannableString("POLICE");
        msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //斜体
        txt.setText(msp);
        mInstance = this;
        settingListener = new SettingListener(this, this);
    }

    /*查是否配置ip端口（保证网络畅通）存在：login。不存在，setting*/
    @Override
    public void initEvents() {
        settingListener.downLoad();
    }

    boolean isFirst = true;

    @Override
    public void downLoad(boolean isTrue, String mes) {
        if (isTrue && isFirst) {
            isFirst = false;
            CopyFile.CopyWltlib(this);
            settingListener.getCards();
            mUtils.IntentPost(LoginActivity.class);//跳转登录
        } else if (isFirst) {
            if(mes!=null) {
                Utils.ToastShort(this, mes);
                mUtils.WriteString(Key.KEY_IP, "");
                mUtils.WriteString(Key.KEY_PORT, "");
                finalDb.deleteAll(TerminalModel.class);
                finalDb.deleteAll(PoliceModel.class);
                finalDb.deleteAll(PersonModel.class);
                finalDb.deleteAll(CompanyModel.class);
                finalDb.deleteAll(ModifiedModel.class);
                finalDb.deleteAll(CodeModel.class);
                Utils.WriteString("CodeChangeTime", "");
            }else{
                Utils.ToastShort(this, "请在后台注册设备信息");
            }
        }
        mInstance.finish();
    }

    @Override
    public void DownKu(boolean isTrue) {

    }
}
